#include <QStringList>
#include "abstract-section-model.h"
#include "section-info.h"
#include "value-result.h"

AbstractSectionModel::AbstractSectionModel(QObject *parent)
    : QAbstractTableModel(parent), m_isModelEditable(true),
      m_isEditingCurrentlyEnabled(false), m_reloadAfterModification(true)
{
}

QVariant AbstractSectionModel::id (int row, int column) const
{
    Q_UNUSED(row);
    Q_UNUSED(column);
    return QVariant();
}

QVariantList AbstractSectionModel::ids (QList<int> rows) const
{
    QVariantList ids;
    for (int i = 0; i < rows.size(); i++)
        ids << id(rows.at(i));
    return ids;
}

QStringList AbstractSectionModel::headerSectionsForAdding() const
{
    QStringList headerSections;
    for (int i = 0; i < columnCount(); i++)
        headerSections.append(headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
    return headerSections;
}

QMap<int,QAbstractItemDelegate*> AbstractSectionModel::delegates() const
{
    return QMap<int,QAbstractItemDelegate*>();
}

QList<QAbstractItemDelegate*> AbstractSectionModel::delegatesForAdding() const
{
    return delegates().values();
}

ValueResult *AbstractSectionModel::canAddData (QVariantList dataList)
{
    Q_UNUSED(dataList);
    ValueResult* result = new ValueResult(this);
    result->setError("");
    return result;
}

ValueResult *AbstractSectionModel::canUpdateData (QVariant value, int row, int column)
{
    Q_UNUSED(value);
    Q_UNUSED(row);
    Q_UNUSED(column);
    ValueResult* result = new ValueResult(this);
    result->setError("");
    return result;
}

ValueResult *AbstractSectionModel::canSetField (QVariant value, int column, int row)
{
    Q_UNUSED(value);
    Q_UNUSED(column);
    Q_UNUSED(row);
    ValueResult* result = new ValueResult(this);
    result->setError("");
    return result;
}

ValueResult *AbstractSectionModel::canSetRow (QVariantList dataList, int row)
{
    Q_UNUSED(dataList);
    Q_UNUSED(row);
    ValueResult* result = new ValueResult(this);
    result->setError("");
    return result;
}

QVariant AbstractSectionModel::correctedValue (QVariant value, int column) const
{
    Q_UNUSED(column);
    return value;
}

bool AbstractSectionModel::isEditingEnabled() const
{
    return m_isEditingCurrentlyEnabled;
}

SectionInfo AbstractSectionModel::sectionInfo() const
{
    return SectionInfo();
}

Qt::ItemFlags AbstractSectionModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    if (m_isModelEditable && m_isEditingCurrentlyEnabled)
        return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    else
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

void AbstractSectionModel::loadData()
{
}

void AbstractSectionModel::loadData(bool result)
{
    if (result)
        loadData();
}

void AbstractSectionModel::addData (QVariantList dataList)
{
    Q_UNUSED(dataList);
}

void AbstractSectionModel::updateData (QVariant data, int row, int column)
{
    Q_UNUSED(data);
    Q_UNUSED(row);
    Q_UNUSED(column);
}

void AbstractSectionModel::removeData (QVariantList dataList)
{
    Q_UNUSED(dataList);
}

void AbstractSectionModel::removeData (QVariant data)
{
    return removeData(QVariantList() << data);
}

void AbstractSectionModel::setSectionInfo(const SectionInfo &sectionInfo)
{
    Q_UNUSED(sectionInfo);
}

void AbstractSectionModel::setLoadingParameters (QVariantList parameters)
{
    Q_UNUSED(parameters);
}

void AbstractSectionModel::setEditingEnabled(bool enabled)
{
    m_isEditingCurrentlyEnabled = enabled;
}

void AbstractSectionModel::setReloadAfterModification(bool enabled)
{
    m_reloadAfterModification = enabled;
}
